package com.istock.union.user.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.istock.base.common.api.model.PageInfo;
import com.istock.base.export.util.DataTranslateUtils;
import com.istock.base.mybatis.utils.MybatisFieldConvertUtils;
import com.istock.union.user.dao.OperLogInfoMapper;
import com.istock.union.user.model.DeptInfo;
import com.istock.union.user.model.OperLogInfo;
import com.istock.union.user.model.OperLogInfoExample;
import com.istock.union.user.model.OperLogQuery;
import com.istock.union.user.service.DeptInfoService;
import com.istock.union.user.service.OperLogInfoService;
import com.istock.union.user.translate.DeptTranslateCallback;

@Repository
public class OperLogInfoServiceImpl implements OperLogInfoService{

	@Autowired
	private OperLogInfoMapper mapper;
	@Autowired
	private DeptInfoService deptInfoService;
	
	public void insertOperLog(OperLogInfo operLogInfo){
		mapper.insertSelective(operLogInfo);
	}
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	public List search(OperLogQuery query, PageInfo page) {
		if (query != null) {
			OperLogInfoExample example = new OperLogInfoExample();
			OperLogInfoExample.Criteria criteria = example.createCriteria();
			if (StringUtils.isNotBlank(query.getMenuName())) {
				criteria.andMenuNameLike("%" + query.getMenuName() + "%");
			}
			if(StringUtils.isNotBlank(query.getFunctionName())) {
				criteria.andFunctionNameLike("%" + query.getFunctionName() + "%");
			}
			if(query.getDeptCodeList() != null && query.getDeptCodeList().size()>0) {
				criteria.andDeptCodeIn(query.getDeptCodeList());
			}
			/*if (StringUtils.isNotBlank(query.getOperMethod())) {
				criteria.andOperMethodLike("%" + query.getOperMethod() + "%");
			}*/
			if (StringUtils.isNotBlank(query.getOperName())) {
				criteria.andOperNameLike("%" + query.getOperName() + "%");
			}
			if (StringUtils.isNotBlank(query.getBeginTime())) {
				Date beginTime;
				try {
					beginTime = DateUtils.parseDate(query.getBeginTime(), "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss");
					criteria.andOperTimeGreaterThan(beginTime);
				} catch (ParseException e) {
					logger.error("can't parser the date for beginTime:[{}]", query.getBeginTime());
				}
			}

			if (StringUtils.isNotBlank(query.getEndTime())) {
				Date date = null;
				try {
					date = DateUtils.parseDate(query.getEndTime(), "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss");
					criteria.andOperTimeLessThan(date);
				} catch (ParseException e) {
					logger.error("can't parser the date for beginTime:[{}]", query.getEndTime());
				}
			}
			if(StringUtils.isNotBlank(query.getOrderByColumn())) {
				//example.setOrderByClause(NamedConverter.HumpToUnderline(query.getOrderByColumn())+" "+query.getIsAsc());
				example.setOrderByClause(MybatisFieldConvertUtils.getInstance().unmarshal("com.istock.union.user.dao.OperLogInfoMapper", "BaseResultMap", query.getOrderByColumn())+" "+query.getIsAsc());
			}else {
				example.setOrderByClause("OPER_TIME DESC");
			}
			mapper.selectByExample(example, page);
			List<Map> list = DataTranslateUtils.translate(page.getData(), "deptCode", "deptName", new DeptTranslateCallback());
			page.setData(list);
			return list;
		}
		return null;
	}

	/**
	 * 根据ID查询操作日志
	 * 
	 * @param id
	 * @return
	 */
	public Map selectOperLogById(String id) {
		OperLogInfo operLog = mapper.selectByPrimaryKey(id);
		Map<String, Object> result = null;
		try {
			result = PropertyUtils.describe(operLog);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.isNotBlank(operLog.getDeptCode())) {
			DeptInfo deptInfo = deptInfoService.selectDeptByCode(operLog.getDeptCode());
			result.put("deptName", deptInfo.getDeptName());
			return result;
		}
		return result;
	}

	/**
	 * 删除操作日志
	 * 
	 * @param ids
	 * @return
	 */
	public int deleteOperLogByIds(List<String> ids) {
		Assert.notEmpty(ids, "ID 不能为空");
		if (ids.size() == 1) {
			return mapper.deleteByPrimaryKey(ids.get(0));
		} else {
			OperLogInfoExample example = new OperLogInfoExample();
			example.createCriteria().andIdIn(ids);
			return mapper.deleteByExample(example);
		}
	}

	/**
	 * 清空表记录
	 */
	public void cleanOperLog() {
		OperLogInfoExample example = new OperLogInfoExample();
		mapper.deleteByExample(example);
	}
}
